require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

task :default => [:test]

namespace :active_background do
  task :setup do
    require File.dirname(__FILE__) + "/../init.rb"
    ActiveBackground::Configuration.instance.setup()
  end
  
  task :server do
    begin
      require File.dirname(__FILE__) + "/../init.rb"
      ActiveBackground::Server.start()
    rescue Exception=>e
      puts e.to_s + '\n\n' + e.backtrace.join('\n')
    end
  end
  
  task :server_in_background do
    require File.dirname(__FILE__) + "/../init.rb"
    ActiveBackground::Server.run_in_background()
  end
  
  task :start_service do
    require File.dirname(__FILE__) + "/../init.rb"
    ActiveBackground::Server.start_windows_service()
  end
  
  task :stop_service do
    require File.dirname(__FILE__) + "/../init.rb"
    ActiveBackground::Server.stop_windows_service()
  end

  task :restart_service do
    require File.dirname(__FILE__) + "/../init.rb"
    ActiveBackground::Server.restart_windows_service()
  end
  
  task :install_service do
    require File.dirname(__FILE__) + "/../init.rb"
    ActiveBackground::Server.install_windows_service()
  end
  
  task :delete_service do
    require File.dirname(__FILE__) + "/../init.rb"
    ActiveBackground::Server.delete_windows_service()
  end
  
  task :quick_start_windows do
    require File.dirname(__FILE__) + "/../init.rb"
    ActiveBackground::Server.quick_start_windows()
  end
  
  desc 'Test the active_background plugin.'
  Rake::TestTask.new(:test) do |t|
    t.libs << File.dirname(__FILE__) + '/../lib'
    t.test_files = FileList[File.dirname(__FILE__) + '/../test/*_test.rb']
    t.verbose = true
  end
  
  desc 'Generate documentation for the active_bookmark plugin.'
  Rake::RDocTask.new(:doc) do |rdoc|
    rdoc.rdoc_dir = File.dirname(__FILE__) + '/../docs'
    rdoc.title    = 'ActiveBackground'
    rdoc.options << '--line-numbers' << '--inline-source' << '--all' << '--line-numbers'
    rdoc.template = 'jamis'
    rdoc.rdoc_files.include(File.dirname(__FILE__) + '/../README')
    rdoc.rdoc_files.include(File.dirname(__FILE__) + '/../lib/**/*.rb')
    rdoc.rdoc_files.include(File.dirname(__FILE__) + '/../lib/jobs/*.rb')
    rdoc.rdoc_files.include(File.dirname(__FILE__) + '/../lib/handlers/*.rb')
    rdoc.rdoc_files.include(File.dirname(__FILE__) + '/../lib/archive_handlers/*.rb')
    rdoc.rdoc_files.include(File.dirname(__FILE__) + '/../script/*.rb')
  end
  
  STATS_DIRECTORIES = [
    %w(Libraries          lib/),
    %w(Unit\ tests      test/),
    %w(Scripts      script/)
  ].collect { |name, dir| [ name, File.dirname(__FILE__) + "/../#{dir}" ] }.select { |name, dir| File.directory?(dir) }
  
  desc "Report code statistics (KLOCs, etc) from the application"
  task :stats do
    require File.dirname(__FILE__) + '/../lib/vendor/code_statistics/code_statistics.rb'
    CodeStatistics.new(*STATS_DIRECTORIES).to_s
  end
end

desc 'Test the active_background plugin.'
Rake::TestTask.new(:test) do |t|
  t.libs << File.dirname(__FILE__) + '/../lib'
  t.test_files = FileList[File.dirname(__FILE__) + '/../test/*_test.rb']
  t.verbose = true
end